<template>
  <div v-if="coreToolStore.switchPostLoginInfo.show">
    <a-dropdown placement="bottomCenter">
      <a-tag color="purple" :style="{ 'font-size': '14px', ...coreStyle.marginLeft(7) }">{{ coreToolStore.switchPostLoginInfo.currPost }}</a-tag>
      <template #overlay>
        <a-menu>
          <a-menu-item v-for="item in coreToolStore.switchPostLoginInfo.postList" :disabled="item.disabled" @click="switchPostLogin(item.loginSide, item.account, item.password, item.postId)">
            <a-tag :color="item.disabled ? 'default' : 'cyan'" :style="{ 'font-size': '14px', ...coreStyle.marginLeft(7) }">{{ item.postName }}</a-tag>
          </a-menu-item>
        </a-menu>
      </template>
    </a-dropdown>
  </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue'

import { mapStores, mapActions } from 'pinia'

import coreTool from '/@/tool/coreTool'
import coreAxios from '/@/tool/coreAxios'

import { useCoreToolStore } from '/@/store/modules/cn/core/tool/coreToolStore'
import { useUserStore } from '/@/store/modules/user'

import type { LoginAO } from '/@/model/cn/core/tool/ao/LoginAO'
import type { LoginVO } from '/@/model/cn/core/tool/vo/LoginVO'

import type { UserInfo } from '/#/store'

import { RoleEnum } from '/@/enums/roleEnum'
import { PageEnum } from '/@/enums/pageEnum'

import { router } from '/@/router'

export default defineComponent({
  name: 'SwitchPostLogin',
  props: {
  },
  emits: [
  ],
  data() {
    return {
    }
  },
  computed: {
    ...mapStores(useCoreToolStore),
  },
  methods: {
    ...mapActions(useUserStore, ['setToken', 'setUserInfo', 'setRoleList', 'setSessionTimeout']),
    async switchPostLogin(loginSide, account, password, postId) {
      this.coreToolStore.login(this.$toParam<LoginAO>({ loginSide: loginSide, account: account, password: password, postId: postId } as LoginAO)).then(async (res: Res<LoginVO>) => {
        if (res.succ) {
          let { msg, accountId, usrId, usrname, avatarUrl, token, aesKey, authList, switchPostLoginInfo, allDictValueMapInfo, allConfMapInfo } = res.data[0]
          coreTool.ls.set('coreToken', token)
          coreTool.ls.set('coreAesKey', coreTool.aesKeyDecode ? coreTool.decodeRSAByPrivateKey(aesKey, coreTool.privateKey) : aesKey)
          coreAxios.defaults.headers.common['Authorization'] = coreTool.ls.get!('coreToken') || 'token'
          coreTool.ls.set('switchPostLoginInfo', switchPostLoginInfo)
          coreTool.ls.set('allDictValueMapInfo', allDictValueMapInfo)
          coreTool.ls.set('fileUrlPrefix', allConfMapInfo.fileUrlPrefix)
          await this.coreToolStore.setSwitchPostLoginInfo(switchPostLoginInfo)
          await this.coreToolStore.setAllDictValueMapInfo(allDictValueMapInfo)
          await this.coreToolStore.setFileUrlPrefix(allConfMapInfo.fileUrlPrefix)
          await this.setToken(token)
          let userInfo: UserInfo = {
            userId: usrId,
            username: account,
            realName: usrname,
            avatar: this.coreToolStore.getFileFullUrl(avatarUrl),
            desc: usrname,
          }
          await this.setUserInfo(userInfo)
          await this.setRoleList(authList as RoleEnum[])
          await this.setSessionTimeout(false)
          await router.replace(PageEnum.BASE_HOME)
          setTimeout(() => { location.reload() }, 500)
          this.$message.success(msg)
        } else {
          this.$message.error(res.msg, 6)
        }
      })
    }
  },
  mounted() {
  }
})
</script>

<style scoped lang="less">

</style>
